//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   EconomicSourceDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:24:50  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese02Econsource;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class EconomicSourceDao extends Dao {

    private static final String CLASS_NAME = EconomicSourceDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * Busca Economic Sources segun code y name cargados en el Teese02Econsource
     * pasado por parametro
     * 
     * @param code
     * @param name
     * @return
     * @throws PersistenceException
     */
    public Collection<Teese02Econsource> findEconomicSources(
                                                             Teese02Econsource economicSource)
            throws PersistenceException {

        String METHOD_NAME = "findEconomicSources";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teese02Econsource> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicSources",
                Teese02Econsource.class);
        query.setParameters(economicSource);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }

    /**
     * Da de alta el Economic Source pasado por parametro
     * 
     * @param economicSource
     * Debe tener cargado el nombre del Economic Source
     * @throws PersistenceException
     */
    public void createEconomicSource(Teese02Econsource economicSource)
            throws PersistenceException {

        String METHOD_NAME = "createEconomicSource";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(economicSource);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Devuelve el economicSource cuyo code es economicSourceCode listo para
     * realizarle las actualizaciones pertinentes
     * 
     * @param plant
     * Planta a la que pertenece el usuario que realiza la actualizacion
     * @param economicSourceCode
     * Code del Economic Source a actualizar
     * @return
     * @throws PersistenceException
     */
    public Teese02Econsource getEconomicSourceForUpdate(
                                                        Teesg01Plant plant,
                                                        Integer economicSourceCode)
            throws PersistenceException {

        String METHOD_NAME = "getEconomicSourceForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teese02Econsource result = null;
        Teese02Econsource pk = new Teese02Econsource();

        pk.setEese02PlantCodeC(plant);
        pk.setEese02EconsourceNumberK(economicSourceCode);
        result = (Teese02Econsource) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Elimina de forma definitiva de la base de datos el economicSource pasado
     * por parametro
     * 
     * @param economicSource
     * Debe haber sido obtenido de la base de datos
     * @throws PersistenceException
     */
    public void deleteEconomicSource(Teese02Econsource economicSource)
            throws PersistenceException {

        String METHOD_NAME = "deleteEconomicSource";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(economicSource);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

}
